data=load_lvd('C:\tmpdata\2011-02-02\GK_test0018.lvd');
iimg=load_iimg_data('C:\tmpdata\2011-02-02\GK_test0018.iimg');

% data1=load_lvd('C:\tmpdata\2011-02-02\GK_test0038.lvd');
% iimg1=load_iimg_data('C:\tmpdata\2011-02-02\GK_test0038.iimg');
data2=load_lvd('C:\tmpdata\2011-02-02\GK_test0039.lvd');
iimg2=load_iimg_data('C:\tmpdata\2011-02-02\GK_test0039.iimg');
% data3=load_lvd('C:\tmpdata\2011-02-02\GK_test0016.lvd');
% iimg3=load_iimg_data('C:\tmpdata\2011-02-02\GK_test0016.iimg');

data=[data1 data2];
clear iimg
iimg(:,:,1:size(iimg1,3))=iimg1;
iimg(:,:,end+1:end+size(iimg2,3))=iimg2;
% iimg(:,:,end+1:end+size(iimg3,3))=iimg3;

aa=data2(2,:);
aa=diff(aa);
aa(aa>5)=aa(aa>5)-10;
aa(aa<-5)=aa(aa<-5)+10;
aa=(smooth(abs(aa),500))>0.002;

frame_times=find(diff(data(4,:))>2.5);

stim_type_ms=round((data(1,:)-1)/4*24);
stim_type_ms=round(20*data(1,:))/10-2;

figure;
imagesc(mean(iimg2(:,:,aa(frame_times(1:5:end))),3)./(mean(iimg2(:,:,~aa(frame_times(1:5:end))),3)));
%imagesc(mean(iimg(:,:,aa(frame_times)),3)./(mean(iimg(:,:,~aa(frame_times)),3)));
colorbar;
colormap gray

pre_ind=circshift(stim_type_ms,[0 -2500]).*(stim_type_ms==-2);

figure
colormap gray
for stim_ind=1:24
    x_pos=mod(stim_ind-1,8);
    y_pos=floor((stim_ind-1)/8);
    axes('position',[x_pos/8 1-(y_pos+1)/3 1/8 1/3])
    tmp=mean(iimg(:,:,stim_type_ms(frame_times([1:5:end]))==stim_ind),3)./mean(iimg(:,:,stim_type_ms(frame_times([1:5:end]))<=0),3);
    imagesc(tmp);
    set(gca,'clim',[-0.001 0.001]+median(tmp(:)))
    axis off
    drawnow
end


figure;
use_ind=stim_type_ms(frame_times([1:5:end]))>0;
control_ind=stim_type_ms(frame_times([1:5:end]))<0;
imagesc(mean(iimg(:,:,use_ind),3)./mean(iimg(:,:,control_ind),3));
colorbar;
colormap gray




